//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
//  StringUtils.cs
//  Created on:      28-feb-2005 0:49:42
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Yawf.Core.Utils {
	public class StringUtils {
		/// <summary>
		/// This method will split a String into an array of values
		/// This method will split by the delimiter given
		/// </summary>
		/// <param name="sourceStr"></param>
		/// <param name="delim"></param>
		/// <returns></returns>
  	public static String[] SplitString(String sourceStr,String delim){
 
			if (sourceStr != null){
	  		return sourceStr.Split(delim.ToCharArray());
			}

			return null;
		}					// SplitString

		public static String[] SplitString(String sourceStr,char delim){
			return SplitString(sourceStr,"" + delim);
		}
		
		/// <summary>
		/// Returns a string with all elements in a array separeted with a
		/// delimiter
		/// </summary>
		/// <param name="a">The array to merge</param>
		/// <param name="fieldName">Delimiter to use</param>
  	public static String Concatenate(Object[] a,char delim){
  		String mergedStr = "";
			int i;
  		
  		for(i=0;a != null && i < a.Length;i++){
  			if (i > 0){
					mergedStr = mergedStr+delim;
  			}
 				mergedStr = mergedStr+a[i].ToString();
  		}
  		
  		return mergedStr;
		}						/* Concatenate */

		public static String FormarRUT(String rut,String dv){
			String formatedRut;
			int rutInt;

			try{
				rutInt = Convert.ToInt32(rut);
				formatedRut = String.Format(new CultureInfo("es-CL"),"{0:###,###,###}-{1}",rutInt,dv);
			}catch(Exception e){
				formatedRut = rut + "-" + dv;
				e.ToString();
			}

			return formatedRut;
		}

		public static String FormarRUT(String rut){
			String rutStr,dvStr;
			String[] values;

			if (rut != null){
				rut = rut.Replace(".","");
				values = StringUtils.SplitString(rut,'-');
				if (values != null && values.Length == 2){
					rutStr = values[0];
					dvStr = values[1];
					return FormarRUT(rutStr,dvStr);
				}else if (rut.Length > 1){
					rutStr = rut.Substring(0,rut.Length - 1);
					dvStr = rut.Substring(rut.Length - 1,1);
					return FormarRUT(rutStr,dvStr);
				}
			}
		
			return rut;
		}

		/// <summary>
		/// Returns index of a string in a List of string. The search is done
		/// that it will find a string that starts with name and then it will
		/// do a split(delimStr) operation on the found string. If the returning 
		/// array contains more or equals number of elements than least, that 
		/// index will be returned
		/// </summary>
		/// <param name="paramNames">list of parameter names to look inside</param>
		/// <param name="name">name of parameter to look for</param>
		/// <param name="least">least number of elements that name should contain 
		/// after doing split</param>
		/// <param name="delimStr">delimeter string to split with</param>
		/// <returns>index if name us found in list, -1 if not</returns>
		public static int FindStringIndex(IList<String> paramNames,String name,int least,String delimStr){
			String tmpName;
			String[] values;
			int i;
			
			for(i = 0; paramNames != null && name != null && i < paramNames.Count; i++){
				tmpName = paramNames[i];
				if (tmpName != null && tmpName.StartsWith(name)){
					values = SplitString(tmpName,delimStr);
					if (values != null && values.Length >= least){
						return i;
					}
				}
			}
			
			return -1;
		}											// FindStringIndex

		/// <summary>
		/// Converts a string value into a bool type
		/// </summary>
		/// <param name="tmpStr">String to convert</param>
		/// <returns>true if </returns>
		public static bool BoolValue(String value) {
			if (!IsEmpty(value)) {
				value = value.ToLower();
				return value == "yes" || value == "y"
						|| value == "si" || value == "s"
						|| value == "true" || value == "t"
						|| value == "1" || value == "on";
			}

			return false;
		}

		/// <summary>
		/// Checks weather a String is null or a empty string(after applying Trim()
		/// </summary>
		/// <param name="value">String to check</param>
		/// <returns>true if string is empty, false otherwsie</returns>
		public static bool IsEmpty(String value) {
			bool test = value == null || value.Trim() == String.Empty;
			return test;
		}
	}										// Class StringUtils
}							// Namespace 
